import constant from '../../constant'
import config from '../../config'
import cookie from '../../core/cookie'
import Base64Codec from '../../api/base64'

/**
 * 认证模块
 */
const moduleAuth = {
  state: {
    isLogin: false,
    user: { // 用户登录信息
      userId: process.env.NODE_ENV === 'production' ? '' : config.USER_ID,
      token: process.env.NODE_ENV === 'production' ? '' : config.USER_TOKEN
    }
  },
  getters: {
    [constant.GET_IS_LOGIN]: state => {
      return state.isLogin
    }
  },
  mutations: {
    [constant.GET_LOGIN_DATA]: state => {
      let userId = cookie.get(constant.LOGIN_DATA_ID)
      let userToken = cookie.get(constant.LOGIN_DATA_TOKEN)
      state.user.userId = userId || state.user.userId || ''
      state.user.token = userToken || state.user.token || ''
      state.user = Base64Codec.LoginVo.encode(state.user)
    },
    [constant.SET_IS_LOGIN] (state, isLogin) {
      state.isLogin = isLogin
    },
    [constant.SET_LOGIN_DATA] (state, user) {
      state.user = Base64Codec.LoginVo.decode(user)
      window.console.log('++++++++++++++++++', state.user)
      state.isLogin = true
      cookie.set(constant.LOGIN_DATA_ID, state.user.userId)
      cookie.set(constant.LOGIN_DATA_TOKEN, state.user.token)
    },
    [constant.DELETE_LOGIN_DATA] (state) {
      cookie.remove(constant.LOGIN_DATA_ID)
      cookie.remove(constant.LOGIN_DATA_TOKEN)
    }
  }
}
export default moduleAuth
